package com.xunwulian.cmpp.pdu;

import com.google.gson.Gson;
import com.xunwulian.base.util.GsonUtil;
import com.xunwulian.cmpp.sms.ByteBuffer;
import com.xunwulian.cmpp.sms.NotEnoughDataInByteBufferException;
import com.xunwulian.cmpp.sms.PDUException;
import com.xunwulian.cmpp.util.CmppConstant;

import java.io.Serializable;

public class Cancel extends Request implements Serializable {

    private static final long serialVersionUID = 3796831609023216872L;

    private String msgId = "";

    public Cancel() {
        super(CmppConstant.CMD_CANCEL);
    }

    protected Response createResponse() {
        return new CancelResp();
    }

    public ByteBuffer getData() {
        ByteBuffer bodyBuf = getBody();
        header.setCommandLength(CmppConstant.PDU_HEADER_SIZE + bodyBuf.length());
        ByteBuffer buffer = header.getData();
        buffer.appendBuffer(bodyBuf);
        return buffer;
    }

    public void setData(ByteBuffer buffer) throws PDUException {
        header.setData(buffer);
        setBody(buffer);
    }

    public ByteBuffer getBody() {
        ByteBuffer buffer = new ByteBuffer();
        buffer.appendString(msgId, 8);
        return buffer;
    }

    public void setBody(ByteBuffer buffer)
            throws PDUException {
        try {
            msgId = buffer.removeStringEx(8);
        } catch (NotEnoughDataInByteBufferException e) {
            e.printStackTrace();
            throw new PDUException(e);
        }
    }

    public String getMsgId() {
        return msgId;
    }

    public void setMsgId(String msgId) {
        this.msgId = msgId;
    }

    public String name() {
        return "CMPP Cancel";
    }


    public String dump() {
        return GsonUtil.toJson(this, this.getClass());
    }
}
